package com.srpingde.hhh;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class ShouHuoXinXiActivity extends Activity {
	EditText phone,address,shouhuo_name;
	TextView phone_tv,address_tv,shouhuo_tv_name;
	String content;
	ImageView back;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shou_huo_xin_xi);
		phone = (EditText) findViewById(R.id.shouhuo_telphone);
		address = (EditText) findViewById(R.id.shouhuo_address);
		phone_tv = (TextView) findViewById(R.id.shouhuo_tv_phone);
		address_tv = (TextView) findViewById(R.id.shouhuo_tv_address);
		shouhuo_tv_name = (TextView) findViewById(R.id.shouhuo_tv_name);
		shouhuo_name = (EditText) findViewById(R.id.shouhuo_name);
		
		////以下增加提示信息
		phone.setText(HhhApp.getInstance().getPhone());//
		address.setText(HhhApp.getInstance().getAddress());//
		shouhuo_name.setText(HhhApp.getInstance().getName());//
		back = (ImageView) findViewById(R.id.back);
		layout();
		if(getIntent().getExtras()!=null){
			content = getIntent().getStringExtra("content");
		}
	}
	
	/*
	 * 手机正则
	 */
	public static boolean isMobileNO(String mobiles) {
		String telRegex = "[1][358]\\d{9}";
		if (TextUtils.isEmpty(mobiles))
			return false;
		else
			return mobiles.matches(telRegex);
	}
	
	/*
	 * 座机正则
	 */
	public static boolean isPhoneNO(String phoneNo) {
		String telRegex = "^0\\d{2,3}-\\d{6,8}$";//010-12345678
		if (TextUtils.isEmpty(phoneNo))
			return false;
		else
			return phoneNo.matches(telRegex);
	}
	
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			try {
				if(!((String)msg.obj).equals("0")){
					JSONObject jsonObject = new JSONObject((String)msg.obj);
					if(jsonObject.getBoolean("return")){
						HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "购买成功");
					}else{
						HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "购买失败");
					}
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	};
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.goumai_btn:
			//电哈号码不为空的时候，正则判断格式
			String nums = phone.getText().toString();
			boolean isNumsOK = true;
			if(!isMobileNO(nums) && !isPhoneNO(nums)){	
				//WeiQuApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "电话号码格式不正确！");
				isNumsOK = false;
			}
			if(!shouhuo_name.getText().toString().equals("") && shouhuo_name.getText().toString() != null){
				if(!phone.getText().toString().equals("") && phone.getText().toString() != null && isNumsOK){					
					if(!address.getText().toString().equals("") && address.getText().toString() != null){					
						String url = HHHInterface.httpHeader + "api.php?tab=address&act=add&name="+shouhuo_name.getText().toString()+"&address="+address.getText().toString()+"&tel="+phone.getText().toString()+"&uid="+HhhApp.getInstance().getUid();
						HHHIml weiQuIml = new HHHIml();
						weiQuIml.loginHttp(ShouHuoXinXiActivity.this,url, 
				    			new AsyncHttpResponseHandler(){
							@Override
							public void onSuccess(String content) {
								super.onSuccess(content);
								Log.e("mytag", content+"================收藏返回的数据");
								try {
									JSONObject jsonObject = new JSONObject(content);
									if(jsonObject.getBoolean("return")){
										new AlertDialog.Builder(ShouHuoXinXiActivity.this).setTitle("提示").setMessage("添加成功").setPositiveButton("确定", new OnClickListener() {
											
											@Override
											public void onClick(DialogInterface dialog, int which) {
												// TODO Auto-generated method stub
												finish();
											}
										}).create().show();
									}else{
										HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "添加失败");
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});	
					}else{
						HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "地址不能为空");
					}
				}else{

					HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "电话号码不能为空且格式正确");
				}
			}else{
				HhhApp.getInstance().showDialog(ShouHuoXinXiActivity.this, "收件人不能为空");
			}
			break;
		}
	}
	public static String retrieveInputStream(HttpEntity httpEntity) {
        int length = (int) httpEntity.getContentLength();
        if (length < 0)
            length = 10000;
        StringBuffer stringBuffer = new StringBuffer();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8);
            char buffer[] = new char[length];
            int count;
            while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
                stringBuffer.append(buffer, 0, count);
            }
        } catch (UnsupportedEncodingException e) {
        } catch (IllegalStateException e) {
        } catch (IOException e) {
        }
        return stringBuffer.toString();
    }
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
		//输入框和文字的padding值
    	int paddingWidth_tv = (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105);
    	int paddingWidth = (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.15);
    	phone.setPadding(paddingWidth, 0, paddingWidth, 0);
    	address.setPadding(paddingWidth, 0, paddingWidth, 0);
    	phone_tv.setPadding(paddingWidth_tv, 0, 0, 0);
    	address_tv.setPadding(paddingWidth_tv, 0, 0, 0);
    	shouhuo_tv_name.setPadding(paddingWidth_tv, 0, 0, 0);
    	shouhuo_name.setPadding(paddingWidth, 0, 0, 0);
	}
}
